-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Reduce React re-renders when checking currentCard()
#3398
Conversation
- `currentCard()` is called multiple times which triggers the expensive `upcomingCardIds()` - Adding a static `currentCard` variable reduced calls (and thus multiple React re-renders)
Removed vultr server and associated DNS entries |
83c2eaa
to
e27818c
Compare
e27818c
to
c4f9bd2
Compare
@@ -48,7 +48,7 @@ const Card: React.FC<Props> = ({ | |||
...props | |||
}) => { | |||
const theme = useTheme(); | |||
const [path, currentCard] = useStore((state) => [ | |||
const [path, visibleNode] = useStore((state) => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throughout this PR I've tired to maintain existing variable names when deconstructing state from the store. I think this minimises the number of changes, and hopefully maintains context within the component. For example, sometimes the currentCard
is referred to as the node
, visibleNode
or currentCard
across differing components.
@@ -379,6 +380,7 @@ export const previewStore: StateCreator< | |||
|
|||
resumeSession(session: Session) { | |||
set({ ...session }); | |||
get().setCurrentCard(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When resuming, we handle the initial state setup here after getting a ReconciliationResponse
Please see #1701 for context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All working as expected for me & pizza feels snappy 👏
Really valuable refactor here even if unfortunately not root of August's issue yet !!
What's the problem?
What's the cause?
When running the application in dev mode, the following two errors generally coincide with the above occurrence -
The stack traces of these errors point to the following lines of code respectively -
planx-new/editor.planx.uk/src/pages/FlowEditor/lib/store/preview.ts
Line 512 in 20ce9d6
https://github.com/theopensystemslab/planx-new/blob/main/editor.planx.uk/src/@planx/components/Section/Public.tsx#L31
After a lot of trial and error debugging these sections it essentially appears that the problem is caused by
upcomingCardIds()
(called when hitting "back") clashing with the rendering of theAnalyticsProvider
(which passed the updatednode
down to it's descendants).What's the solution?
currentCard()
upcomingCards()
and is ultimately responsible for displaying a card to the userrecord()
)currentCard()
is called as a shorthand to get the current node Id, so I've made a new property in the store to hold this variableupcomingCardIds()
, just referring to the storedcurrentCard()
from the last call.node
from theAnalyticsProvider
, and just store in Zustand